﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class CategoryChildDAO
    {
        private DBService _db = null;
        public CategoryChildDAO(String ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public DataTable GetCateChild()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from CategoryChild", false, CommandType.Text);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetCateChild_ByIDParrent(int IDCateParrent)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from CategoryChild where IDCateParrent = @IDCateParrent", 
                    new string[]{"@IDCateParrent"},
                    new object[]{IDCateParrent}, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // get menu Child by id IDCateChild
        public CategoryChild GetCateChild_ByIDChild(int IDCateChild)
        {
            CategoryChild CateChild = new CategoryChild();
            try
            {
                DataTable dt = null;
                dt = _db.ExeSP_DataTable("select * from CategoryChild where IDCateChild = @IDCateChild",
                    new string[] { "@IDCateChild" },
                    new object[] { IDCateChild }, CommandType.Text);

                foreach (DataRow dr in dt.Rows)
                {
                    CateChild.IDCateChild = IDCateChild;
                    CateChild.CateChildName = dr["CateChildName"].ToString();
                    CateChild.IDCateParrent = Convert.ToInt32 (dr["IDCateParrent"].ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return CateChild;
        }

        public DataTable InsUpCateChild(CategoryChild CateChild, int mode)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_CategoryChild_InsUp", 
                    new string[]{"@IDCateChild", "@CateChildName","@IDCateParrent", "@mode"},
                    new object[]{CateChild.IDCateChild, CateChild.CateChildName, CateChild.IDCateParrent, mode}, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        // Xoa menu Child
        public bool DeleteMenuChild(int IDCateChild)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("delete CategoryChild where IDCateChild = @IDCateChild",
                    new string[] { "@IDCateChild" },
                    new object[] { IDCateChild },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
    }
}
